﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using System.Collections.Generic;

namespace HRProject.Code.DAO
{
    public class RoleDAO : BaseDAO
    {
        public RoleDAO()
            : base ()
        {
        }

        public List<BLRole> searchRoles(BLRole filter)
        {
            List<BLRole> result = new List<BLRole>();
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@id", filter.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@name", filter.Name, ParameterDirection.Input));
            DataTable data = select("usp_searchRoles", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                BLRole role = new BLRole();
                role.Id = (Guid)data.Rows[i]["RoleId"];
                role.Name = (string)data.Rows[i]["RoleName"];
                role.Description = (string)data.Rows[i]["Description"];
                role.IconUrl = (string)data.Rows[i]["ImageUrl"];
                role.Department = new BLDepartment();
                role.Department.Id = Guid.Empty;
                role.Department.Name = string.Empty;
                result.Add(role);
            }
            return result;
        }

        public List<BLRole> searchRolesInDepartment(BLRole filterRole)
        {
            List<BLRole> result = new List<BLRole>();
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@id", filterRole.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@name", filterRole.Name, ParameterDirection.Input));
            condition.Add(new BaseCondition("@departmentId", filterRole.Department.Id, ParameterDirection.Input));
            DataTable data = select("usp_searchRolesInDepartment", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                BLRole role = new BLRole();
                role.Id = (Guid)data.Rows[i]["RoleId"];
                role.Name = (string)data.Rows[i]["RoleName"];
                role.Description = (string)data.Rows[i]["Description"];
                role.IconUrl = (string)data.Rows[i]["ImageUrl"];
                role.Department = new BLDepartment();
                role.Department.Id = (Guid)data.Rows[i]["DepartmentId"];
                role.Department.Name = (string)data.Rows[i]["DepartmentName"];
                role.DataSignature = (long)data.Rows[i]["DataSignature"];
                role.CreateDate = (DateTime)data.Rows[i]["CreateDate"];
                role.CreateUserId = (Guid)data.Rows[i]["CreateUserId"];
                role.UpdateDate = (DateTime)data.Rows[i]["UpdateDate"];
                role.UpdateUserId = (Guid)data.Rows[i]["UpdateUserId"];
                result.Add(role);
            }
            return result;
        }

        public List<BLRole> getAvailableRolesToAdd(BLDepartment department)
        {
            List<BLRole> result = new List<BLRole>();
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@departmentId", department.Id, ParameterDirection.Input));
            DataTable data = select("usp_getAvailableRolesToAdd", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                BLRole role = new BLRole();
                role.Id = (Guid)data.Rows[i]["RoleId"];
                role.Name = (string)data.Rows[i]["RoleName"];
                role.Department = new BLDepartment();
                result.Add(role);
            }
            return result;
        }

        public BLRole getRole(string roleName)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@name", roleName, ParameterDirection.Input));
            DataTable data = select("usp_getRole", condition);
            if (data.Rows.Count > 0)
            {
                BLRole result = new BLRole();
                result.Id = (Guid)data.Rows[0]["RoleId"];
                result.Name = (string)data.Rows[0]["RoleName"];
                result.Department = new BLDepartment();
                return result;
            }
            return null;
        }

        public int updateRole(BLRole role)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            int errorCode = 0;
            condition.Add(new BaseCondition("@id", role.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@name", role.Name, ParameterDirection.Input));
            condition.Add(new BaseCondition("@description", role.Description, ParameterDirection.Input));
            condition.Add(new BaseCondition("@imageUrl", role.IconUrl, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_updateRole", condition);
            return result;
        }

        public int insertRoleToDepartment(BLRole role)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            int errorCode = 0;
            condition.Add(new BaseCondition("@id", role.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@departmentId", role.Department.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@createUserId", Membership.GetUser().ProviderUserKey.ToString(),ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", role.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode",errorCode,ParameterDirection.Output));
            int result = update("usp_addRoleToDepartment", condition);
            return result;
        }

        public int deleteRoleFromDepartment(BLRole role)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            int errorCode = 0;
            condition.Add(new BaseCondition("@id", role.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@departmentId", role.Department.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", Membership.GetUser().ProviderUserKey.ToString(), ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", role.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_deleteRoleFromDepartment", condition);
            return result;
        }

        public int deleteRole(BLRole role)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            int errorCode = 0;
            condition.Add(new BaseCondition("@id", role.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_deleteRole", condition);
            return result;
        }
    }
}
